﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;


namespace Workshop
{
    public partial class FormLogin : Form
    {
        private string type;        //save it as a global value
        public FormLogin()
        {
            InitializeComponent();
        }

        public FormLogin(string type)
        {
            InitializeComponent();      
            this.type = type;       //get the parameter of which type the use choose
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (File.Exists("Account.xml"))     //judge that whether the xml is exist or not
            {
                int i;
                XmlDocument doc = new XmlDocument();
                doc.Load("Account.xml");        //load the xml file if it exists
                if (type == "PD")       //if the user choose PD type to login, then go here
                {
                    String path1 = "//Accounts/PDs/PD/ID";      //based on the type to select the value
                    XmlNodeList nodeList = doc.SelectNodes(path1);
                    for (i = 0; i < nodeList.Count; i++)
                    {
                        if (nodeList[i].InnerText == txtID.Text)      //check whether the ID is true or not
                        {
                            if (nodeList[i].ParentNode.ChildNodes[1].InnerText == txtPassword.Text)     //if ID is true, then check the password is correct or not
                            {
                                 //if the password is correct, then jump to the main page
                                FormCourseinformation formCourseinformation = new FormCourseinformation();
                                formCourseinformation.Show();
                                this.Hide();
                                break;
                            }
                            else
                            {
                                MessageBox.Show("Password wrong", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);      //if the password is wrong
                                break;
                            }
                        }
                    }
                    if (i == nodeList.Count)
                    {
                        MessageBox.Show("This ID doesn't exists!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);     //if the ID is not true
                    }
                }

                else
                {
                    String path1 = "//Accounts/HeadTAs/HeadTA/ID";      //based the type to select
                    XmlNodeList nodeList = doc.SelectNodes(path1);
                    for (i = 0; i < nodeList.Count; i++)
                    {
                        if (nodeList[i].InnerText == txtID.Text)        //check the id is true or not
                        {
                            if (nodeList[i].ParentNode.ChildNodes[1].InnerText == txtPassword.Text)     //if ID is true, check the passwork is correct or not
                            {
                                 // if the password is correct , jump to the other page
                                FormCoursetime formCourseTime = new FormCoursetime();
                                formCourseTime.Show();
                                this.Hide();

                                break;
                            }
                            else
                            {
                                MessageBox.Show("Password wrong", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);      //if the password is wrong
                                break;
                            }
                        }
                    }
                    if (i == nodeList.Count)
                    {
                        MessageBox.Show("This ID doesn't exists!","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);     //if the ID is wrong
                    }
                }
            }
        }

        private void btnGoBack_Click(object sender, EventArgs e)
        {
            FormHeadPixel headPixel = new FormHeadPixel();
            headPixel.Show();
            this.Close();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
